package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.domain.FlashPromotionProductRelation;
import com.itheima.domain.Product;
import com.itheima.service.IProductService;
import com.itheima.service.SmsFlashPromotionProductRelationService;
import com.itheima.util.Result;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SmsFlashPromotionProductRelationVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class SmsFlashPromotionProductRelationManager {

    @DubboReference
    SmsFlashPromotionProductRelationService relationService;
    @DubboReference
    IProductService iProductService;


    //分页查询不同场次关联及商品信息
    public Result list(Integer pageNum, Integer pageSize, Integer flashPromotionId, Integer flashPromotionSessionId) {

        PageBeanVo pageBeanVo = relationService.list(pageNum, pageSize, flashPromotionId, flashPromotionSessionId);

        List<FlashPromotionProductRelation> selectList = (List<FlashPromotionProductRelation>) pageBeanVo.getList();
        List<SmsFlashPromotionProductRelationVo> voList = new ArrayList<>();


        if (selectList != null) {
            for (FlashPromotionProductRelation relation : selectList) {
                SmsFlashPromotionProductRelationVo vo = new SmsFlashPromotionProductRelationVo();
                BeanUtil.copyProperties(relation, vo);
                vo.setProduct(relationService.getProductById(relation.getProductId()));
                voList.add(vo);
            }
        }
        pageBeanVo.setList(voList);

        return new Result(true, 200, "操作成功", pageBeanVo);
    }

    //批量选择商品添加关联
    public Result create(List<FlashPromotionProductRelation> relationList) {
        if (relationList.size() > 0) {
            for (FlashPromotionProductRelation relation : relationList) {
                relationService.create(relation);
            }
        }
        return new Result(true, 200, "操作成功", 1);
    }
    //删除关联

    public Result delete(Integer id) {
        relationService.delete(id);
        return new Result(true, 200, "操作成功", 1);
    }

    //修改关联信息
    public Result update(Long id, FlashPromotionProductRelation relation) {
        Product pmsProduct = iProductService.findById(relation.getProductId());
        Integer stock = pmsProduct.getStock();//剩余数量
        Integer count = relation.getFlashPromotionCount();//限时购数量
        Integer limit = relation.getFlashPromotionLimit();//个人限购数量
        BigDecimal price = pmsProduct.getPrice();//原价
        BigDecimal price1 = relation.getFlashPromotionPrice();//秒杀价格
        if (count != null && limit != null) {
            if (count < limit) {
                return new Result(false, 403, "操作失败，限时购数量小于个人限购数量", 1);
            }
        }
        if (stock != null && count != null) {
            if (stock < count) {
                return new Result(false, 403, "操作失败，剩余数量小于限时购数量", 1);
            }
        }
        if (stock != null && limit != null) {
            if (stock < limit) {
                return new Result(false, 403, "操作失败，剩余数量小于个人限购数量", 1);
            }
        }
        if (price != null && price1 != null) {
            if (price.compareTo(price1) == -1) {
                return new Result(false, 403, "操作失败，秒杀价格大于原价", 1);
            }
        }
        relation.setId(id);
        relationService.update(relation);
        return new Result(true, 200, "操作成功", 1);
    }

    //获取管理商品促销信息
    public Result selectById(Integer id) {
        FlashPromotionProductRelation relation = relationService.selectById(id);
        return new Result(true, 200, "操作成功", relation);
    }
}
